package vn.lumi.smarthome.load;

import vn.lumi.smarthome.io.database.TBL_Scene;
import vn.lumi.smarthome.io.model.Scene;
import vn.lumi.smarthome.io.model.Time;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;

public class SceneListLoader extends AsyncTaskLoader<Scene[]>{

	private int mRoomID;
	
	public SceneListLoader(Context context,int roomID) {
		super(context);
		mRoomID=roomID;
	}

	@Override
	public Scene[] loadInBackground() {
		Scene[] sceneList=null;
		String selection=TBL_Scene.ROOM_ID+"=?";
		String[] selectionArgs=new String[]{mRoomID+""};
		Cursor cursor=getContext().getContentResolver().query(TBL_Scene.CONTENT_URI_SCENE, null, selection, selectionArgs, null);
		if(cursor==null) return null;
		
		sceneList=new Scene[cursor.getCount()];
		int i=0;
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
			int id=cursor.getInt(cursor.getColumnIndex(TBL_Scene._ID));
			int roomID=cursor.getInt(cursor.getColumnIndex(TBL_Scene.ROOM_ID));
			String name=cursor.getString(cursor.getColumnIndex(TBL_Scene.NAME));
			int stateID=cursor.getInt(cursor.getColumnIndex(TBL_Scene.STATE));
			int mode = cursor.getInt(cursor.getColumnIndex(TBL_Scene.MODE));
			int hour = cursor.getInt(cursor.getColumnIndex(TBL_Scene.HOUR));
			int minute = cursor.getInt(cursor.getColumnIndex(TBL_Scene.MINUTE));
			Time time = new Time(hour, minute);
			Scene scene=new Scene(id, roomID, name, stateID, mode, time);
			sceneList[i]=scene;
			i++;
		}
		return sceneList;
	}

}
